package com.example.demo;

import java.util.concurrent.locks.Lock;

import static com.example.demo.test.LockManager.LOCKS;


public class TestLockTask implements LockTask<Integer> {


    @Override
    public LockTaskResult<Integer> apply(Lock lock, Long startTime, Long timeOut, LockTaskCallback lockTaskCallback) {
        try {
            LOCKS.put(lock, new LockData(lock, startTime, timeOut));
            Thread.sleep(1000 * 10);
            LockTaskResult<Integer> lockTaskResult = LockTaskResult.lockSuccessAndResult(1);
            LOCKS.computeIfPresent(lock, (k, v) -> {
                v.setFinished(true);
                return v;
            });
            lockTaskCallback.onComplete(lock);
            return lockTaskResult;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return LockTaskResult.lockSuccessAndResult(0);
        }
    }
}
